﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GameDevelopHelper.Common;
using GameDevelopHelper.Configer;

namespace GameDevelopHelper
{
    public partial class frmProjectManager : Form
    {
        public frmProjectManager()
        {
            InitializeComponent();
        }

        private void frmProjectManager_Load(object sender, EventArgs e)
        {
            List<DataRow> ProjectRows = ProjectConfigerWrapper.GetProjectList();
            foreach (DataRow row in ProjectRows)
            {
                //添加到列表
                ListViewItem item = new ListViewItem(row["ProjectName"].ToString());
                item.SubItems.Add(row["ProjectUrl"].ToString());
                ListViewItem NewItem = lsvProjectList.Items.Add(item);
                NewItem.Tag = row;
            }
        }

        //载入自己（不能打开项目）
        public void LoadOnlyManager()
        {
            btnLoadProject.Visible = false;
            this.ShowDialog();
        }

        //载入自己（可以打开项目）
        public void LoadManager()
        {
            btnLoadProject.Visible = true;
            this.ShowDialog();
        }

        private void btnAddProject_Click(object sender, EventArgs e)
        {
            AddProject();
        }

        //添加项目
        private void AddProject()
        {
            frmAddProject frm = new frmAddProject();
            frm.ShowDialog();
            string strProjectName = frm.GetSelectedProjectName();
            string strProjectUrl = frm.GetSelectedProjectUrl();
            bool bResult = frm.GetAddResult();
            frm.Close();

            if (bResult == false)
            {
                return;
            }

            if (strProjectName.Length == 0 || strProjectUrl.Length == 0)
            {
                StringTable.ResAlert("ADD_PROJECT_FAILED", StringTable.ALERT_TYPE.ERROR);
                return;
            }

            //添加到列表
            ListViewItem item = new ListViewItem(strProjectName);
            item.SubItems.Add(strProjectUrl);
            ListViewItem NewItem = lsvProjectList.Items.Add(item);
            NewItem.Selected = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDeleteProject_Click(object sender, EventArgs e)
        {
            if (lsvProjectList.SelectedItems.Count == 0)
            {
                return;
            }

            bool bSuccess = ProjectConfigerWrapper.DelProject((DataRow)lsvProjectList.SelectedItems[0].Tag);
            if (bSuccess == false)
            {
                StringTable.ResAlert("DEL_PROJECT_FAILED", StringTable.ALERT_TYPE.ERROR);
                return;
            }

            lsvProjectList.Items.Remove(lsvProjectList.SelectedItems[0]);
        }
    }
}
